<?
require_once( 'image-resizer-0-base.php' );

class GdImageResizer extends ImageResizer 
{
	function renderContent($name,$filename,$new_w,$new_h){
		
		//global $gd2;
		
		$system=explode(".",$name);
		if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
		if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
		
		$old_x=imageSX($src_img);
		$old_y=imageSY($src_img);
		if ( is_null ( $new_h) )
		{
			$thumb_w = $new_w;
			$thumb_h = ($new_w/$old_x)  * $old_y;
		} elseif ( is_null ( $new_w) )
		{
			$thumb_h = $new_h;
			$thumb_w = ($new_h/$old_y)  * $old_x;
		} else 
		{
			if ($old_x > $old_y) {
				$thumb_w=$new_w;
				$thumb_h=$old_y*($new_h/$old_x);
			}
			if ($old_x < $old_y) {
				$thumb_w=$old_x*($new_w/$old_y);
				$thumb_h=$new_h;
			}
			if ($old_x == $old_y) {
				$thumb_w=$new_w;
				$thumb_h=$new_h;
			}
			if ($old_x < $new_w) {
				$thumb_w=$old_x;
				$thumb_h=$old_y;
			}
			if ($old_y < $new_h) {
				$thumb_w=$old_x;
				$thumb_h=$old_y;
			}
		}
		// If smaller than max size, make a smaller image
		/*
		if ($gd2==""){
			$dst_img=ImageCreate($thumb_w,$thumb_h);
			imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		} else {
		*/
		$dst_img = @imageCreateTrueColor($thumb_w,$thumb_h);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		//}
		if (preg_match("/png/",$system[1])){
			$result = imagepng($dst_img,$filename);
		} else {
			$result = imagejpeg($dst_img,$filename,90);
		}
		imagedestroy($dst_img);
		imagedestroy($src_img);
		return $result;
	}
}
?>